const {app, BrowserWindow, Menu, MenuItem} = require('electron');
const path = require('path');

//Create native application menus and context menus.
//https://www.electronjs.org/docs/latest/api/menu

app.on('ready', () => {
  let win = new BrowserWindow({
    //https://www.electronjs.org/docs/latest/api/browser-window#new-browserwindowoptions
    width: 802,
    height: 550,

    webPreferences: {
      nodeIntegration: true,
      // contextIsolation: true,
      // enableRemoteModule: false,
      // devTools: true
    }
  });

  //template (MenuItemConstructorOptions | MenuItem)[]
  const menu = new Menu();
  const itemArr = [
    {
      label: '打开'
      ,id:'open'
      , click: () => {
        console.log('打开');
      }
    }, {
      label: '重命名'
      ,id:'rename'
      ,click: () => {
        console.log('重命名');
      }
    },
    {
      label: '删除'
      ,id:'delete'
      ,click: () => {
        console.log('删除');
      }
    }
  ]
  itemArr.forEach(item => {
    menu.append(new MenuItem(item))
  });

  menu.popup({
    x:100,
    y:100
  });

  const selectedItem = menu.getMenuItemById('open');
  console.log('selectedItem:',selectedItem);
  /*
    selectedItem: <ref *1> MenuItem {
      label: '鎵撳紑',
      id: 'open',
      click: [Function (anonymous)],
      submenu: null,
      type: 'normal',
      role: null,
      accelerator: null,
      icon: null,
      sublabel: '',
      toolTip: '',
      enabled: true,
      visible: true,
      checked: false,
      acceleratorWorksWhenHidden: true,
      registerAccelerator: true,
      commandId: 32,
      menu: EventEmitter {
        insertItem: [Function: insertItem],
        insertCheckItem: [Function: insertCheckItem],
        insertRadioItem: [Function: insertRadioItem],
        insertSeparator: [Function: insertSeparator],
        insertSubMenu: [Function: insertSubMenu],
        setIcon: [Function: setIcon],
        setSublabel: [Function: setSublabel],
        setToolTip: [Function: setToolTip],
        setRole: [Function: setRole],
        clear: [Function: clear],
        getIndexOfCommandId: [Function: getIndexOfCommandId],
        getItemCount: [Function: getItemCount],
        getCommandIdAt: [Function: getCommandIdAt],
        getLabelAt: [Function: getLabelAt],
        getSublabelAt: [Function: getSublabelAt],
        getToolTipAt: [Function: getToolTipAt],
        isItemCheckedAt: [Function: isItemCheckedAt],
        isEnabledAt: [Function: isEnabledAt],
        worksWhenHiddenAt: [Function: worksWhenHiddenAt],
        isVisibleAt: [Function: isVisibleAt],
        popupAt: [Function: popupAt],
        closePopupAt: [Function: closePopupAt],
        commandsMap: { '32': [Circular *1], '33': [MenuItem], '34': [MenuItem] },
        groupsMap: {},
        items: [ [Circular *1], [MenuItem], [MenuItem] ]
      }
    }

  */



  win.loadFile(path.resolve(__dirname, 'index.html'))
});

